$("#head").load("header.html",function () {})
$("#foot").load("footer.html",function () {})
var vues = new Vue({
    el:'#index',
    data:{
        //公用url
        url:'http://api.456mobi.com/upload/',
        imgData:'',
        block:[],
        //快讯保存
        newsFlash:[],
        breviary:[],
        whole:[],
        //切换按钮ID
        idlist:[],
        //资讯保存
        information:[],
        //专访保存
        interview:[],
        //分析保存
        analysis:[],
        carousel:{
            title : '',
            con : ''
        }
    },
    ready:function(){
        var that = this;

        $("#wx_icon").hover(function(){
            $("#wx_ii").show()
        },function(){
            $("#wx_ii").hide()
        });
//图片接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/index/index',
            type:'get',
            success: function(res){
                if(res.code == '1'){
                    that.imgData = res.data
                    that.carousel.title = res.data.pic3[0].title
                    that.carousel.con = res.data.pic3[0].content
                    $('.propaganda').css({'background':'url("'+ that.url + res.data.pic2[0].image +'") no-repeat center'})
                    setTimeout(function () {
                        $('#myCarousel .item').eq(0).addClass('active')
                        $('#myCarousel').carousel({
                            interval: 4000
                        })
                        $('#myCarousel').on('slide.bs.carousel', function (event) {
                             var $hoder = $('#myCarousel').find('.item'),$items = $(event.relatedTarget)
                             var getIndex = $hoder.index($items)
                            that.carousel.title = res.data.pic3[getIndex].title
                            that.carousel.con = res.data.pic3[getIndex].content
                        })
                    },50)
                }else{
                    alert('图片获取失败')
                }
            }
        })
//资讯接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/Information/category',
            type:'get',
            success: function(ress){
                if(ress.code == '1'){
                    var res = ress
                    res.data.list.forEach(function (item,index) {
                        item.more = JSON.parse(item.more)
                        var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                        time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                        // time[1] = time[1].substring(2,time[1].length)
                        time[1] = time[1].split(':')
                        item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1]
                    })
                    that.block = res.data
                    for(var x in that.block.category){
                        that.idlist.push(that.block.category[x].id)
                        for(var c in that.block.category[x].child){
                            that.idlist.push(that.block.category[x].child[c].id)
                        }
                    }
                }else{
                    alert('资讯获取失败')
                }
            }
        })
//快讯接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/Information/get_newsflash',
            type:'get',
            success: function(ress){
                if(ress.code == '1'){
                    var res = ress.data
                    res.forEach(function (item,index) {
                        var nTime = Date.parse(new Date()) - (item.issue_time*1000)
                        nTime = nTime/1000
                        var day = Math.floor(nTime/86400)
                        var hour = Math.floor(nTime%86400/3600)
                        var minute = Math.floor(nTime%86400%3600/60)
                        var time = ' '
                        if(day > 0){
                            time += day + '天'
                            time += hour + '小时'
                            time += minute + '分钟前'
                        }else{
                            if(hour > 0){
                                time += hour + '小时'
                            }
                            time += minute + '分钟前'
                        }

                        item.issue_time = time;
                        //保存内容
                        that.whole[index] = item.content;
                        if(item.content.length > 60){
                            that.breviary [index] = item.content.substring(0,60) + " ..."
                            item.content = that.breviary[index]
                            item.opentext = '【展开】'
                        }else{
                            that.breviary [index] = item.content
                            item.opentext = ' '
                        }
                        /*   that.whole[index] = item.content;
                          var r = /[^\x00-\xff]/g;
                          var len = item.content.replace(r,"rr").length;
                          var n=60;
                          //alert(len);
                     /*  if(len > n){
                              var m = Math.floor(n/2);
                              var _newstr = "";
                              for(var i=_newlen;i<=_strlen;i++){
                                  var tmpstr = str.substr(0,i);
                                  if(fucchecklength(tmpstr)>n){
                                      return _newstr;
                                      break;
                                  } else {
                                      _newstr = tmpstr;
                                  }
                              }
                              for(var i=m; i<this.length; i++) {
                                  item.content = item.content.substr(0, i)
                                  if(item.content.substr(0, i).replace(r, "rr").length>=n) {
                                      item.content.substr(0, i) +"...";
                                  }
                              }
                              alert(item.content);

                              that.breviary [index] = item.content
                              item.content = that.breviary[index]
                              item.opentext = '【展开】'
                          }else{
                              that.breviary [index] = item.content
                              item.opentext = ''
                          }*/
                    })
                    that.newsFlash = res
                }else{
                    alert('快讯获取失败')
                }
            }
        })
//项目专访接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/Information/index',
            type:'post',
            data:{
                category_id : 2
            },
            success: function(ress){
                var res = ress
                if(res.msg == '获取成功'){
                    res.data.forEach(function (item,index) {
                        item.more = JSON.parse(item.more)
                        var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                        time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                        // time[1] = time[1].substring(2,time[1].length)
                        time[1] = time[1].split(':')
                        item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1]
                        that.interview.push(item)
                    })
                }else{
                    // alert(res.msg)
                }
            }
        })
//行情分析接口
        $.ajax({
            url:'http://api.456mobi.com/api/?s=home/Information/index',
            type:'post',
            data:{
                category_id : 3
            },
            success: function(ress){
                var res = ress
                if(res.msg == '获取成功'){
                    res.data.forEach(function (item,index) {
                        item.more = JSON.parse(item.more)
                        var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                        time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                        // time[1] = time[1].substring(2,time[1].length)
                        time[1] = time[1].split(':')
                        item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1]
                        that.analysis.push(item)
                    })
                }else{
                    // alert(res.msg)
                }
            }
        })
//点击切换事件
        $('.tabs>div').click(function () {
            if($('.tabs .active').html() == '资讯'){
                that.information = that.block.list
            }else if($('.tabs .active').html() == '项目专访'){
                that.interview = that.block.list
            }else{
                that.analysis = that.block.list
            }
            $('.tabs>div').removeClass('active')
            $(this).addClass('active')


            if($(this).html() == '资讯'){
                $('.tabs .arrow').css({'left':'65px'})
                that.block.list = that.information
            }/*else if($(this).html() == '项目专访'){
                $('.tabs .arrow').css({'left':'174px'})
                that.block.list = that.interview
            }*/else{
               // $('.tabs .arrow').css({'left':'285px'})
                $('.tabs .arrow').css({'left':'174px'})
                that.block.list = that.analysis
            }
        })
        var scr = function () {
            var winPos = $(window).scrollTop();
            if(winPos >= 400){
                $('#totop').css({'display':'inline-block'})
            }else{
                $('#totop').css({'display':'none'})
            }
        }
        $(window).scroll(function(){
            scr();
        })
        scr();
    },
    methods:{
        open:function (ID,opentext) {
            var that = this
            var dom = '.list #'+ID+' .text'
            if(opentext == '【收起】'){
                that.newsFlash.forEach(function (item,index) {
                    if(item.id == ID){
                        $(dom).css({"maxHeight":"150px"})
                        setTimeout(function () {
                            item.opentext = '【展开】'
                            item.content = that.breviary[index]
                        },300)
                    }
                })
            }else{
                that.newsFlash.forEach(function (item,index) {
                    if(item.id == ID){
                        $(dom).css({"maxHeight":"500px"})
                        item.opentext = '【收起】'
                        item.content = that.whole[index]
                    }else{
                        if(item.opentext != ' '){
                            item.opentext = '【展开】'
                            item.content = that.breviary[index]
                        }
                    }
                })
            }
        },
        news_details:function (id,img_url) {
            $(location).attr('href', 'news_details.html?id='+id+'&img_url='+img_url);
        },
        more:function () {
            var that = this
            $.ajax({
                url:'http://api.456mobi.com/api/?s=home/Information/index',
                type:'post',
                data:{
                    category_id : $('.tabs .active').attr("id"),
                    post_id : that.block.list[that.block.list.length-1].id
                },
                success: function(ress){
                    var res = ress
                    if(res.msg == '获取成功'){
                        res.data.forEach(function (item,index) {
                            item.more = JSON.parse(item.more)
                            var time = new Date(item.published_time * 1000).toLocaleString('chinese',{hour12:false}).split(' ')
                            time[0] = time[0].replace(/\//g,"-").substr(5,time[0].length)
                            // time[1] = time[1].substring(2,time[1].length)
                            time[1] = time[1].split(':')
                            item.published_time = time[0] + ' ' + time[1][0] + ':' + time[1][1]
                            that.block.list.push(item)
                        })
                    }else if (res.msg=='暂无更多'){
                        $('.btn').css({'color':'#aaa','border':"1"})
                        $('.btn').html('暂无更多')
                    }else{
                        // alert(res.msg)
                    }
                }
            })
        },
    }
});
function fucchecklength(strtemp) {
    var i, sum;
    sum = 0;
    for (i = 0; i < strtemp.length; i++) {
        if ((strtemp.charcodeat(i) >= 0) && (strtemp.charcodeat(i) <= 255)) {
            sum = sum + 1;
        } else {
            sum = sum + 2;
        }
    }
    return sum;
}
function subcharstr(str,n){
    var _len=fucchecklength(str,n);
    if(_len>n){
        var _newlen=math.floor(n/2);
        var _strlen=str.length;
        var _newstr = "";
        for(var i=_newlen;i<=_strlen;i++){
            var tmpstr = str.substr(0,i);
            if(fucchecklength(tmpstr)>n){
                return _newstr;
                break;
            } else {
                _newstr = tmpstr;
            }
        }
    }else{
        return str;
    }
}